# -*- coding: utf-8 -*-
'''
version 2.0
__author__: YB.
contact yb80102@126.com
'''
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)
import xml.dom.minidom
import xlwt, os

def fomat_content(s):
    slist = [i.strip() for i in s.split('</p>')]
    res = '\n'.join([i for i in slist if i])
    reslist = [i.strip() for i in res.split('<p>') if i.strip()]
    res = '\n'.join(reslist)
    return res
def get_testsuite_info(xmlf):
    testname = xmlf.getAttribute('name')
    if xmlf.getElementsByTagName('details')[0].childNodes:
        testdescribe = xmlf.getElementsByTagName('details')[0].childNodes[0].nodeValue
    else:
        testdescribe = 'none'
    return fomat_content(testname), fomat_content(testdescribe)
def get_testcase(testcase):
    testcasename = testcase.getAttribute('name')
    testimportance = fomat_content(testcase.getElementsByTagName('importance')[0].childNodes[0].nodeValue)
    if testcase.getElementsByTagName('summary')[0].childNodes:
        testcasesummary = fomat_content(testcase.getElementsByTagName('summary')[0].childNodes[0].nodeValue)
    else:
        testcasesummary = 'none'
    if testcase.getElementsByTagName('preconditions')[0].childNodes:
        testprecondition = fomat_content(testcase.getElementsByTagName('preconditions')[0].childNodes[0].nodeValue)
    else:
        testprecondition = 'none'
    testactions = []
    testexpections = []
    if testcase.getElementsByTagName('steps'):
        teststeps = testcase.getElementsByTagName('steps')[0].getElementsByTagName('step')
        for teststep in teststeps:
            if teststep.getElementsByTagName('actions')[0].childNodes:
                testaction = fomat_content(teststep.getElementsByTagName('actions')[0].childNodes[0].nodeValue)
            else:
                testaction = 'none'
            if teststep.getElementsByTagName('expectedresults')[0].childNodes:
                testexpection = fomat_content(teststep.getElementsByTagName('expectedresults')[0].childNodes[0].nodeValue)
            else:
                testexpection = 'none'
            testactions.append(testaction)
            testexpections.append(testexpection)
    return testcasename, testimportance, testcasesummary, testprecondition, testactions, testexpections
def fillinExcel_testname(excelf, testinfo):
    sheet0 = excelf.add_sheet(u'testname', cell_overwrite_ok=True)
    sheet0.write(0, 0 , u'测试集（功能模块）名称')
    sheet0.write(1, 0, u'测试集描述')
    sheet0.write(0, 1, testinfo[0])
    sheet0.write(1, 1, testinfo[1])
def fillinExcel_subtestsuite_basicinfo(sheet, testsuiteinfo):
    sheet.write(0, 0 , u'子功能名称')
    sheet.write(1, 0 , u'子功能详细描述')
    sheet.write(0, 1 , testsuiteinfo[0])
    sheet.write(1, 1 , testsuiteinfo[1])
    sheet.write(2, 0 , u'用例名称')
    sheet.write(2, 1 , u'优先级（1-3）低中高')
    sheet.write(2, 2 , u'用例描述')
    sheet.write(2, 3 , u'预置条件')
    sheet.write(2, 4 , u'测试步骤')
    sheet.write(2, 5 , u'期望结果')
def fillinExcel_subtestsuite(excelf, testsuiteinfo, subtestsuite):
    sheet = excelf.add_sheet(testsuiteinfo[0], cell_overwrite_ok=True)
    fillinExcel_subtestsuite_basicinfo(sheet, testsuiteinfo)
    testcases = subtestsuite.getElementsByTagName('testcase')
    writeline = 3
    for testcase in testcases:
        testcasecontent = get_testcase(testcase)
        sheet.write(writeline, 0, testcasecontent[0])
        sheet.write(writeline, 1, testcasecontent[1])
        sheet.write(writeline, 2, testcasecontent[2])
        sheet.write(writeline, 3, testcasecontent[3])
        step_action = '----split line----\n'.join(testcasecontent[4])
        sheet.write(writeline, 4, step_action)
        step_exception = '----split line----\n'.join(testcasecontent[5])
        sheet.write(writeline, 5, step_exception)
        writeline += 1
def main_fillinExcel(xmlf, excelfname):
    excelf = xlwt.Workbook(encoding='utf-8', style_compression=0)
    testsuiteinfo = get_testsuite_info(xmlf)
    fillinExcel_testname(excelf, testsuiteinfo)
    subtestsuites = xmlf.getElementsByTagName('testsuite')
    for subtestsuite in subtestsuites:
        subtestsuiteinfo = get_testsuite_info(subtestsuite)
        fillinExcel_subtestsuite(excelf, subtestsuiteinfo, subtestsuite)
    excelf.save(excelfname)

testsuiteElementlist = []
def get_testsuite_element(ele):
    global testsuiteElementlist
    tag = 0
    ele_childs = ele.getElementsByTagName('testsuite')
    if ele_childs:
        for i in ele_childs:
            ele_child_childs = i.getElementsByTagName('testsuite')
            if ele_child_childs:
                tag += 1
                get_testsuite_element(i)
    else:
        tag += 1
    if not tag:
        if ele not in testsuiteElementlist:
            testsuiteElementlist.append(ele)
def convert2excel(xmlfname):
    if 'resultOFexcel' not in os.listdir('./'):
        os.mkdir(r'./resultOFexcel/')
    DOMTree = xml.dom.minidom.parse(xmlfname)
    xmlf = DOMTree.documentElement
    get_testsuite_element(xmlf)
    _count = 0
    for ele in testsuiteElementlist:
        excelfname = './resultOFexcel/' + ele.getAttribute('name') + '.xls'
        main_fillinExcel(ele, excelfname)
        print ele.getAttribute('name').decode('utf8').encode('gbk')\
        ,'转换完毕，excel文件resultOFexcel文件夹中！'.decode('utf8').encode('gbk')
        _count += 1
    print xmlfname, (" 共转换成%d个EXCEL测试用例集！"%_count).decode('utf8').encode('gbk')


if __name__ == '__main__':
    count = 0
    for i in os.listdir('./'):
        if i.find('.xml')!= -1:
            xmlfname = i
            convert2excel(xmlfname)
            count += 1
    print ('共转换XMl文件%d个'%count).decode('utf8').encode('gbk')
    raw_input('press ENTER to exit')











